package com.sjsoft.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.ClipData.Item;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sjsoft.cache.ImageLoader;
import com.sjsoft.constant.Constant;

import com.sjsoft.app.video.R;
import com.sjsoft.model.messageItem;

public class MessageAdapter extends ArrayAdapter<messageItem> implements Constant{

	private  static List<messageItem> list;
	private   Context context;
	public ImageLoader imageLoader;	
	 
	 public MessageAdapter(Context context, List<messageItem> list) {
	        super(context, R.id.list_chat_content, list);
	        this.context = context;
	        this.list = list;
	        imageLoader=new ImageLoader(context);	
	        
	    }
	    
	    static class msgViewHolder {
	        protected TextView content;	       
	        protected ImageView avatar;	       
	    }
	    
	  public void addMoreItem(messageItem item)
	  {
		  list.add(item);
	  }
	    
	 
	 @Override
	    public View getView( int position, View convertView, ViewGroup parent) {
	     
		 if(list == null)
			 return convertView;
		 msgViewHolder viewHolder = null;
		 messageItem item = list.get(position); 		 
		 
		 if(item != null)
		 {
			 if (convertView == null ) 
			 {
	            LayoutInflater inflator = ((Activity) context).getLayoutInflater();	
	            convertView = inflator.inflate(R.layout.item_message, null);
	            
	            convertView.setClickable(false);		           
	            viewHolder = new msgViewHolder();	                 
	            //if(item._type == 0)
	            //{
	            	viewHolder.content = (TextView) convertView.findViewById(R.id.chat_content);                             
	                viewHolder.avatar = (ImageView) convertView.findViewById(R.id.avatar_chat);
	                
	                convertView.setTag(viewHolder);
		            //convertView.setTag(R.id.chat_content, viewHolder.content);
		            //convertView.setTag(R.id.avatar_chat, viewHolder.avatar);
	           // }
	            //else
	           // {
	            //	viewHolder.content = (TextView) convertView.findViewById(R.id.reply_content); 
	            //	convertView.setTag(viewHolder);
	            //	convertView.setTag(R.id.reply_content, viewHolder.content);
	           // }    
	         }
	         else 
	         {	            
	            viewHolder = (msgViewHolder) convertView.getTag();
	         }
						
	         if(viewHolder != null)
	         {
	        	 
	        	 if(item._type == 0)
	        	 {
	            	convertView.setBackgroundResource(R.drawable.bg_chat);
	            	if(item._imgMember != null && imageLoader != null && viewHolder.avatar != null)
			        	imageLoader.DisplayImage(DOWNLOAD_SERVER + item._imgMember, viewHolder.avatar);
	        	 }
	            else
	            	convertView.setBackgroundResource(R.drawable.bg_reply);	      
		        
		        if(viewHolder.content != null)
		        	viewHolder.content.setText(item._message);
	         }
		 }
	        
	   return convertView;
	 }
	 
}
